/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.MyConnectUnit;
import DTO.Customer;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Quan_Nuce
 */
public class BCustomer {
    MyConnectUnit connect;
    public BCustomer() {
        this.connect = DAL.DAL.getDAL();
    }
    public HashSet<Customer>getCustomers(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("Customer",condition,OderBy);
        HashSet<Customer> customers = new HashSet<Customer>();
        while(result.next()){
            Customer customer = new Customer();
            customer.setCustomerID(result.getInt("CustomerID"));
            customer.setAccountNo(result.getString("AccountNo"));
            customer.setFullName(result.getString("FullName"));
            customer.setAddress(result.getString("Address"));
            customer.setPhone(result.getString("Phone"));
            customer.setSex(result.getString("Sex"));
            customer.setEmail(result.getString("Email"));
            customer.setCreatedDate(result.getString("CreatedDate"));
            
            customers.add(customer);
        }
        return customers;
    }
    
  public HashSet<Customer>getCustomers(String condition) throws Exception{
        return getCustomers(condition,null);
    }
  public HashSet<Customer>getCustomers() throws Exception{
        return getCustomers(null);
    }
   
   public Customer getByID(int id)throws Exception{
        HashSet<Customer>customers = this.getCustomers("CustomerID = "+ id);
        if(customers.size()>0)
            return customers.toArray(new Customer[customers.size()])[0];
        return null;
       
   }
   public void Insert(Customer customer)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("CustomerID", customer.getCustomerID());
        map.put("AccountNo", customer.getAccountNo());
        map.put("FullName", customer.getFullName());
        map.put("Address", customer.getAddress());
        map.put("Phone", customer.getPhone());
        map.put("Sex", customer.getSex());
        map.put("Email", customer.getEmail());
        map.put("CreatedDate", customer.getCreatedDate());
        this.connect.Insert("Customer", map);
       
   }
   public void Inserts(HashSet<Customer> customers)throws Exception{
        for(Customer customer:customers)
            this.Insert(customer);
       
   }
   public void Update(Customer customer)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("AccountNo", customer.getAccountNo());
        map.put("FullName", customer.getFullName());
        map.put("Address", customer.getAddress());
        map.put("Phone", customer.getPhone());
        map.put("Sex", customer.getSex());
        map.put("Email", customer.getEmail());
        map.put("CreatedDate", customer.getCreatedDate());
        this.connect.Update("Customer", map,"CustomerID="+customer.getCustomerID());
       
   }
   public void Updates(HashSet<Customer> customers)throws Exception{
        for(Customer customer:customers)
            this.Update(customer);
       
   }
   public void Delete(Customer customer) throws Exception{
        this.connect.Delete("Customer", "CustomerID="+customer.getCustomerID());
    }
       public void Deletes(HashSet<Customer> customers) throws Exception {
        for(Customer customer:customers)
            this.Delete(customer);
   }
} 
    
 


